<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/7/23
 * Time: 13:37
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Goods extends Base
{
    /**
     * 消息首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){

        if(request()->isAjax ()){
            extract(input());
            $where = [

            ];
            if(isset($keyword) && $keyword != ''){
                $where['good_name|st_name|spp_name|sstyle_name'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($good_status) && $good_status != "")
            {
                $where['good_status'] = $good_status;
            }
            if(isset($good_t_id) && $good_t_id != "")
            {
                $where['good_t_id'] = $good_t_id;
            }
            if(isset($good_pp_id) && $good_pp_id != "")
            {
                $where['good_pp_id'] = $good_pp_id;
            }
            if(isset($good_style_id) && $good_style_id != "")
            {
                $where['good_style_id'] = $good_style_id;
            }
            if(isset($good_s_t_id) && $good_s_t_id != "")
            {
                $where['good_s_t_id'] = $good_s_t_id;
            }
            if(isset($good_s_pp_id) && $good_s_pp_id != "")
            {
                $where['good_s_pp_id'] = $good_s_pp_id;
            }
            $field = 'g.*,t_name,pp_name,st_name,spp_name,sstyle_name';
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_store_goods')->alias('g')
                ->join('up_store_types st','g.good_t_id=st.st_id','LEFT')
                ->join('up_store_pps spp','g.good_pp_id=spp.spp_id','LEFT')
                ->join('up_store_style sstyle','g.good_style_id=sstyle.sstyle_id','LEFT')
//                ->join('up_store s','g.good_s_id=s.s_id','LEFT')
                ->join('up_pp pp','g.good_s_pp_id=pp.pp_id','LEFT')
                ->join('up_types t','g.good_s_t_id=t.t_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_store_goods')->alias('g')
                ->join('up_store_types st','g.good_t_id=st.st_id','LEFT')
                ->join('up_store_pps spp','g.good_pp_id=spp.spp_id','LEFT')
                ->join('up_store_style sstyle','g.good_style_id=sstyle.sstyle_id','LEFT')
//                ->join('up_store s','g.good_s_id=s.s_id','LEFT')
                ->join('up_pp pp','g.good_s_pp_id=pp.pp_id','LEFT')
                ->join('up_types t','g.good_s_t_id=t.t_id','LEFT')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('good_sort desc,good_time desc')
                ->field($field)
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $men_dian = Db::table('up_store')
                        ->where('s_id','in',trim($v['good_s_id'],','))
                        ->column('s_name');
                    $lists[$k]['s_names'] = implode($men_dian,',');
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $datas = $this->getTypePPStyle();
        $this->assign('types',$datas[0]);
        $this->assign('pps',$datas[1]);
        $this->assign('styles',$datas[2]);
        $this->assign('type1',$datas[3]);
        $this->assign('pp1',$datas[4]);
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function goodAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            if(!isset($good_s_id) || !$good_s_id) return json(['code' => 100, 'msg' => '请选择门店']);
            $data = [
                'good_s_id' => ',' . implode($good_s_id,',') . ',',
                'good_name' => $good_name,
                'good_s_t_id' => $good_s_t_id,
                'good_s_pp_id' => $good_s_pp_id,
                'good_thumb' => $good_thumb,
                'good_t_id' => $good_t_id,
                'good_pp_id' => $good_pp_id,
                'good_style_id' => $good_style_id,
                'good_money_name' => $good_money_name,
                'good_money_min' => $good_money_min,
                'good_money_max' => $good_money_max,
                'good_sort' => $good_sort,
                'good_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_store_goods')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $datas = $this->getTypePPStyle();
        $this->assign('types',$datas[0]);
        $this->assign('pps',$datas[1]);
        $this->assign('styles',$datas[2]);
        $this->assign('type1',$datas[3]);
        return $this->fetch();
    }
    /**
     * 编辑
     * @return mixed
     */
    public function goodEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            if(!isset($good_s_id) || !$good_s_id) return json(['code' => 100, 'msg' => '请选择门店']);
            $data = [
                'good_s_id' => ',' . implode($good_s_id,',') . ',',
                'good_name' => $good_name,
                'good_s_t_id' => $good_s_t_id,
                'good_s_pp_id' => $good_s_pp_id,
                'good_thumb' => $good_thumb,
                'good_t_id' => $good_t_id,
                'good_pp_id' => $good_pp_id,
                'good_style_id' => $good_style_id,
                'good_money_name' => $good_money_name,
                'good_money_min' => $good_money_min,
                'good_money_max' => $good_money_max,
                'good_sort' => $good_sort,
                'good_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_store_goods')->where('good_id',$good_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $good_id = input('good_id');
        $info = Db::table('up_store_goods')
            ->where('good_id',$good_id)
            ->find();
        $info['good_s_id'] = explode(',',trim($info['good_s_id']));
        $this->assign([
            'info' => $info,
        ]);
        $datas = $this->getTypePPStyle($info['good_s_t_id'],$info['good_s_pp_id']);
        $this->assign('types',$datas[0]);
        $this->assign('pps',$datas[1]);
        $this->assign('styles',$datas[2]);
        $this->assign('type1',$datas[3]);
        $this->assign('pp1',$datas[5]);
        $stores = Db::table('up_store')
            ->where('s_pp_id',$info['good_s_pp_id'])
            ->where('s_status',1)
            ->field('s_id,s_name')
            ->select();
        $this->assign('stores',$stores);
        return $this->fetch();
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateGoodSort(){
        $id = input('id');
        $field = input('field');
        $param = [
            $field => input('value'),
        ];
        $res = Db::table('up_store_goods')->where('good_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function goodDel(){
        $nt_id = input('id');
        $res = Db::table('up_store_goods')->where('good_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 冻结启用
     * @return mixed|\think\response\Json
     */
    public function changeGoodStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'good_status'=>input('num'),
        ];
        $res = Db::table('up_store_goods')->where('good_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 置顶or不置顶
     * @return mixed|\think\response\Json
     */
    public function changeGoodTop(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'good_top'=>input('num'),
        ];
        $res = Db::table('up_store_goods')->where('good_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 主页面推荐or不推荐
     * @return mixed|\think\response\Json
     */
    public function changeIndexTop(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'good_index'=>input('num'),
        ];
        $res = Db::table('up_store_goods')->where('good_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    //-----------------------------------商品图册----------------------------------------------------------------------
    /**
     * 参数
     * @return mixed|\think\response\Json
     */
    public function newBanner(){
        if(request()->isAjax ()){
            extract(input());
            $where = [
                'pb_pp_id' => session('good_id')
            ];
            if(isset($keyword) && $keyword != ''){
                $where['pb_title|pb_title_up'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($banner_type) && $banner_type != "")
            {
                $where['pb_type'] = $banner_type;
            }
            if(isset($pb_where) && $pb_where != "")
            {
                $where['pb_where'] = $pb_where;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('good_banner')->where($where)->count();// 获取总条数
            $lists = Db::name('good_banner')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('pb_where asc,pb_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $good_id = input('good_id');
        session('good_id',$good_id);
        return $this->fetch('store/banner');
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $cpd_content1 = trim($cpd_content1,', ');
            if(!$cpd_content1){
                return json(['code' => 100, 'msg' => '上传内容不能为空！']);
            }
            $cpd_content1 = explode(',',$cpd_content1);
            $data = [];
            foreach ($cpd_content1 as $k => $v){
                $mww = [
                    'pb_title' => $pb_title,
                    'pb_pp_id' => session('good_id'),
                    'pb_type' => $pb_type,
                    'pb_sort' => $pb_sort,
                    'pb_where' => $pb_where,
                    'pb_url' => $v,
                    'pb_time' => date('Y-m-d H:i:s')
                ];
                if($pb_type == 1){
                    $mww['pb_title_up'] = $pb_title_up;
                }
                $data[] = $mww;
            }
            $res = Db::table('up_good_banner')->insertAll($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $type = input('type');
        $this->assign('type',$type);
        return $this->fetch('store/banneradd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pb_title' => $pb_title,
                'pb_where' => $pb_where,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            $pb_type = Db::table('up_good_banner')->where('pb_id',$pb_id)->value('pb_type');
            if($pb_type != 2){
                $data['pb_url'] = $pb_url;
            }else{
                $data['pb_url'] = $video;
            }
            if($pb_type == 1){
                $data['pb_title_up'] = $pb_title_up;
            }
            $res = Db::table('up_good_banner')->where('pb_id',$pb_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $pb_id = input('pb_id');
        $info = Db::name('good_banner')
            ->where('pb_id',$pb_id)
            ->find();
        $this->assign([
            'banner' => $info,
        ]);
        return $this->fetch('store/banneredit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $nt_id = input('id');
        $res = Db::table('up_good_banner')->where('pb_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function batchDel(){
        $nt_id = input('ids');
        $res = Db::table('up_good_banner')->where('pb_id','in',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateBannerSort(){
        $id = input('id');
        $param = [
            'pb_sort'=>input('value'),
        ];
        $res = Db::name('good_banner')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}